<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright © 2005-2008, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo Core
*
* @package		Ineo
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/
class ineo_core
{
	/**
	* Setup Variables:
	*/
	var $ipsclass     = null;    // ip.board ipsclass
	var $api          = null;    // the ineo component file ( components_public/ineo.php )
	var $application  = 'ipb';   // left over from when we were going to support IPN/IPD.
	var $class_prefix = 'ineo_'; // the prefix all ineo libs/modules will use for their class names.
	var $modules      = array(); // all loaded ineo modules ( usually only one )
	var $libs         = array(); // all loaded ineo libraries
	var $version      = null;    // current version info
	var $base_url     = '';      // base url to ineo ( http://domain.com/index.php?autocom=ineo& )
	
	/**
	* Initialization
	*/
	function init( &$api , $app , $path )
	{
		$this->api          =& $api;
		$this->ipsclass     =& $api->ipsclass;
		$this->application  = $app;
		$this->sys_path     = $path;
		
		// Every page will need lib_global - it *is* global afterall:
		$this->load_library( 'global' );
		
		// Load global templates:
		$this->load_template('global');
		
		// Load language file
    	$this->ipsclass->load_language('lang_ams');
		
    	// Set base url:
    	$this->base_url = $this->api->base_url;

		// Set up title and nav:
    	$this->title = $this->ipsclass->vars['board_name'] . ' - ' . $this->ipsclass->vars['ams_page_title'];
				
		// Require Version File:
		require_once(ROOT_PATH.'sources/ineo/version.php');
        $this->version = $version;
	}
	
	/**
	* System run ( called -after- ipsclass, etc. are passed ).
	*/
	function run()
	{	
    	$this->build_nav();
        
    	// Create global menu:
    	$this->menu_block( 'ams_navigation' );
    	$this->menu_block_section( 'ams_nmain' , array( 
                               							'ams_home_link'   => '',
        											 	'ams_new_article' => 'mod=article&op=new_art',
        											 	'ams_random'      => 'mod=article&op=random',
        											 	'ams_categories'  => 'mod=category&op=catlist',
													    'ams_search'      => 'mod=global&op=full_search',
        												)
        						  );
        
                
        if( $this->ipsclass->vars['ams_watchlists'] )
        {
        	$this->menu_block_section( 'ams_myopts' , array( 
        												'ams_my_watchcats' => 'mod=global&op=cat_watchlist',
        												'ams_my_watcharts' => 'mod=global&op=art_watchlist',
        												)
        						  		);   
        }
        
        if( $this->ipsclass->member['g_ams_ismod'] )
        {
        	$this->menu_block_section( 'ams_nmod' , array( 
        												'ams_rev_manager' => 'mod=moderator&op=revisions',
        												'ams_mq_link'     => 'mod=moderator&op=mq_list',
        												)
        						  		);        						  
        }
                
        $this->menu_add_block();
        
        if( $this->ipsclass->vars['ams_qs_box'] )
        {
        	$this->menu_block( 'ams_quicksearch' );

			// Dan (12th June 06): Moved HTML for quicksearch to a template.
       		$this->menu_block_addcode( $this->templates['skin_ams_global']->quick_search_box() );

        	$this->menu_add_block();
		}
		
		$this->convert_op_shortcuts();
		
		if ( $this->perm_check() == true )
		{
		  $this->op_handle();
		}
		else
		{
			// todo: language file
			$this->libs['global']->error($this->ipsclass->lang['ams_perm_error'],$this->ipsclass->lang['ams_perm_error_text']);
		}
				
		$this->do_output();
	}
	
	/**
	* Load library file ( stored in /lib )
	*
	* @todo Add error message 'error_load_library'
	*/
	function load_library( $library_name )
	{
		$library_name = preg_replace('/([^a-zA-Z0-9_]+)/','',$library_name);
		
		// Have we loaded it already?
		if( isset( $this->libs[$library_name] ) )
		{
			return $this->libs[$library_name];
		}
		
		// No:
		if( file_exists( $this->sys_path . 'libraries/lib_' . $library_name . '.php' ) )
		{
			// The library file exists, load it:
			require_once( $this->sys_path . 'libraries/lib_' . $library_name . '.php' );
			
			// Make the class name:
			$class_name = $this->class_prefix . 'lib_' . $library_name;
					
			// Does the class exist?
			if( class_exists( $class_name ) )
			{
				// Yes - Create an instance:
				$this->libs[$library_name] = new $class_name();
				
				// Pass this class & ipsclass to it:
				$this->libs[$library_name]->ipsclass =& $this->ipsclass;
				$this->libs[$library_name]->core     =& $this;
				
				// Return it:
				return $this->libs[$library_name];
			}
			else
			{
				// Class does not exist:
				$this->error = 'error_load_library';
				$this->error();
			}
		}
		else
		{
			// The library file does not exist, something's up - let's error:
			$this->error = 'error_load_library';
			$this->error();
		}
	}
	
	/**
	* Load module file ( stored in /modules )
	*
	* @todo Add error message 'error_load_module'
	*/
	function load_module( $module_name )
	{
		$module_name = preg_replace('/([^a-zA-Z0-9_]+)/','',$module_name);
		
		// dirty ho hack:
		$module_name = ($module_name == 'xml') ? 'dynamic' : $module_name;
		
		// Have we loaded it already?
		if( isset( $this->modules[$module_name] ) )
		{
			return $this->modules[$module_name];
		}
		
		// No:
		if( file_exists( $this->sys_path . 'modules/mod_' . $module_name . '.php' ) )
		{
			// The module file exists, load it:
			require_once( $this->sys_path . 'modules/mod_' . $module_name . '.php' );
			
			// Make the class name:
			$class_name = $this->class_prefix . 'mod_' . $module_name;
			
			// Does the class exist?
			if( class_exists( $class_name ) )
			{
				// Yes - Create an instance:
				$this->modules[$module_name] = new $class_name();
				
				// Pass this class & ipsclass to it:
				$this->modules[$module_name]->ipsclass =& $this->ipsclass;
				$this->modules[$module_name]->core     =& $this;
				
				$this->modules[$module_name]->module_init();
				
				// Return it:
				return $this->modules[$module_name];
			}
			else
			{
				// Class does not exist:
				$this->error = 'error_load_module';
				$this->error();
			}
		}
		else
		{
			// The module file does not exist, something's up - let's error:
			$this->error = 'error_load_module';
			$this->error();
		}
	}
	
	/**
	* General error throwing fun!
	* $error will be lang key for the error message.
	*/
	function error( $message = null, $title = null )
	{
		$this->libs['global']->error( $title, $message );
	}
	
	/**
	* Do output - Final call back to the calling component to finalize and display.
	*/
	function do_output()
	{
		$this->api->nav = $this->nav;
		
		if( empty($this->no_wrapper) )
		{
			$this->out = $this->templates['skin_ams_global']->global_wrapper($this->menu,$this->out,$this->copyright());
		}
		
		$this->api->do_output( $this->out );
	}
	
	/**
	* Call the API function to load a template group for use:
	*
	* Also doubles as a loading function for $this->templates[], 
	* which will allow it to access ( IPB EG: $this->ipsclass->compiled_templates[] )
	*/
	function load_template( $group )
	{
	    $groups = array( 'categories' => 'skin_ams_categories', 'articles' => 'skin_ams_articles', 'global' => 'skin_ams_global', 'mod' => 'skin_ams_mod' );
	    
	    if( isset( $groups[$group] ) )
	    {
			$load = $groups[$group];
			$alias = $group;
	    }
	    
	    $ret =& $this->api->load_template( $load );
	    
	    if( empty( $this->templates ) or !is_array( $this->templates ) )
	    {
			$this->templates =& $this->ipsclass->$ret;
	    }
	    
	    if( !isset( $this->templates[$alias] ) )
	    {
			$this->templates[$alias] =& $this->templates[$load];
	    }
	}

	
	/**
	* Figure out what the incoming call is, where we're going and what to call.
	*/
	function op_handle()
	{
	    // Create incoming arrays:
	    $module = array( 'ipb' => 'mod' , 'ipd' => 'mod' );
	    $action = array( 'ipb' => 'op' );
	    
	    // Get the module id:
	    $mod_id = $this->ipsclass->input[$module[$this->application]];
	    
	    // Get the action id:
	    $act_id = $this->ipsclass->input[$action[$this->application]];
		$act_id = preg_replace('/([^a-zA-Z0-9_]+)/','',$act_id);
	    
	    // Load the module:
	    $this->load_module( $mod_id );
	    
	    // Run the action:
	    $this->modules[$mod_id]->$act_id();
	}
	
	/**
	* Check perms, make sure you can do what you're trying to do. Yarr.
	*/
  function perm_check()
  {
    $op = $this->ipsclass->input['op'];

    // Check that the ams is not offline:
    if( !empty($this->ipsclass->vars['ams_offline_msg']) and !$this->ipsclass->member['g_access_offline'] )
    {
		$this->ipsclass->load_language('lang_error');
      	$this->out = $this->templates['global']->offline($this->ipsclass->vars['ams_offline_msg']);
		$this->no_wrapper = true;
		$this->do_output();
		return false;
    }

    // Check if the user is allowed to view ineo at all:
    if( ! $this->ipsclass->member['g_ams_view'] )
    {
      $this->permission_error = 'g_ams_view';
      return false;
    }

    // If it's an xml function, we'll have to get 'do' instead:
    $op = ( $op == 'dynamic' ) ? $this->ipsclass->input['do'] : $op;

    // Require Edit Permissions:
    $g_ams_edit = array( 'category_manager' , 'edit' , 'edit_sub' , 'catman' );

    // Require Mod Permissions:
    $g_ams_ismod = array( 'revisions' , 'rev_del' , 'rev_restore' , 'delete', 'mq_list', 'mq_art_app',
    'mq_art_den', 'mq_con_app', 'mq_con_den' );

    // Requires Add Permissions:
    $g_ams_add = array( 'new_art' , 'add' );

    // Requires an int ID:
    $g_id = array( 'cat' , 'article' , 'edit' , 'rev_list' , 'watch_add_art' , 'watch_add_cat' , 'watch_cat_delete' , 'watch_art_delete' , 'rev_view' , 'mq_con_app' ,'mq_con_den' , 'mq_art_app', 'mq_art_app' , 'art_print' );

    if( in_array( $op, $g_ams_edit ) and !$this->ipsclass->member['g_ams_edit'] )
    {
      $this->permission_error = 'g_ams_edit';
      return false;
    }

    if( in_array( $op, $g_ams_ismod ) and !$this->ipsclass->member['g_ams_ismod'] )
    {
      $this->permission_error = 'g_ams_ismod';
      return false;
    }

    if( in_array( $op, $g_ams_add ) and !$this->ipsclass->member['g_ams_add'] )
    {
      $this->permission_error = 'g_ams_add';
      return false;
    }

    if( in_array( $op, $g_id ) and ( empty( $this->ipsclass->input['id'] ) or !is_numeric( $this->ipsclass->input['id'] ) ) )
    {
      $this->permission_error = 'g_id';
      return false;
    }
    else
    {
      $this->ipsclass->input['id'] = intval( $this->ipsclass->input['id'] );
    }

    return true;
  }
	
	/**
	* Convert operation shortcuts, like showcat=, etc. to their full versions:
	*/
	function convert_op_shortcuts()
	{
	    // Set a default to show idx:
	    if( !isset($this->ipsclass->input['mod']) )
	    {
	        $this->ipsclass->input['mod'] = 'global';
	    }
	    if( !isset($this->ipsclass->input['op']) )
	    {
	        $this->ipsclass->input['op'] = 'index';
	    }
	    
	    // Set up show category shortcut:
	    if( !empty($this->ipsclass->input['showcat']) )
	    {
	        $this->ipsclass->input['mod'] = 'category';
	        $this->ipsclass->input['op']  = 'cat';
	        $this->ipsclass->input['id']  = $this->ipsclass->input['showcat'];
	    }
	    
	    if( !empty($this->ipsclass->input['showarticle']) )
	    {
	        $this->ipsclass->input['mod'] = 'article';
	        $this->ipsclass->input['op']  = 'view';
	        $this->ipsclass->input['id']  = $this->ipsclass->input['showarticle'];
	    }
	    
	    if( !empty($this->ipsclass->input['downloadfile']) )
	    {
	        $this->ipsclass->input['mod'] = 'article';
	        $this->ipsclass->input['op']  = 'attach_download';
	        $this->ipsclass->input['id']  = $this->ipsclass->input['downloadfile'];
	    }
	
		// XML is a bit of a stupid module name, considering we don't use XML for our AJAX
		// requests. Renamed the module to dynamic, which is at least somewhat better
		if( $this->ipsclass->input['mod'] == 'xml' )
		{
			$this->ipsclass->input['mod'] = 'dynamic';
		}
	}
	
	function copyright()
	{
		$copy  = '<div style="text-align: center;">Powered by <a href="http://code.google.com/p/ineoipb">Ineo</a> v' . $this->version['human'] . ' &copy; 2005-08, Dan Cryer &amp; William McGann.</div>';
		
		return $copy;
	}
    
    
    function menu_block( $lang_id )
	{
		$this->menu_block_title = $lang_id;
	}
	
	function menu_block_section( $lang_id , $links = array() )
	{
		$link_html = '';
		
		if( is_array( $links ) )
		{
				
			foreach( $links as $title => $url )
			{
				$ltitle = ( empty($this->ipsclass->lang[$title]) ) ? $title : $this->ipsclass->lang[$title];
				$link_html .= $this->templates['global']->menu_link( $url , $ltitle );
			}
		
		}
		else
		{
		
			$link_html = $links;
		
		}
		
		$this->menu_sections[] = $this->templates['global']->menu_title( $this->ipsclass->lang[$lang_id] , $link_html );	
	}
	
	function menu_block_addcode( $section_html )
	{
		$this->menu_sections[] = $section_html;
	}
	
	function menu_add_block()
	{
		$block_html = '';
		
		foreach( $this->menu_sections as $section )
		{
			$block_html .= $section;
		}
		
		$this->menu .= $this->templates['global']->menu_box( $this->menu_block_title , $block_html );
		
		$this->menu_sections = array();
		$this->menu_block_title = '';
	}
	
	function ams_error( $message , $title = 'ams_gen_error' , $link = array( 'lang' =>'ams_return_to_idx' , 'link' => 'autocom=ineo' ) )
	{	
		$error = array(
		'title' => $this->ipsclass->lang[$title],
		'message' => $this->ipsclass->lang[$message],
		'link'  => $link,
		);
		
		$this->out .= $this->templates['global']->general_error( $error );
	}
	
	function build_nav()
	{
		$this->nav_add( $this->ipsclass->vars['ams_page_title'] , '' );
		
		if( $this->ipsclass->input['mod'] == 'category' )
		{
		     $this->nav_add( $this->ipsclass->lang['ams_categories'] , 'mod=category&op=catlist' );
		}
		
		if( $this->ipsclass->input['op'] == 'new' )
		{	
		    $this->nav_add( $this->ipsclass->lang['ams_new_article'] , 'mod=article&op=new_art' );
        }
	}
	
	function nav_add( $title , $link )
	{
		$this->nav[] = "<a href='{$this->base_url}{$link}'>{$title}</a>";
	}
}

?>